package com.sk.cardeal.act.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.act.home.activity.ReachActivity;
import com.sk.cardeal.adapter.OrderAdapter;
import com.sk.cardeal.base.BaseFragment;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.OrderForm;
import com.sk.cardeal.entity.OrderFormList;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.view.MultiStateView;
import com.sk.cardeal.view.SimpleMultiStateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by sk on 2017/12/18.
 */

public class AlreadyReachFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, HandleDataCallBack {
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    @BindView(R.id.SimpleMultiStateView)
    SimpleMultiStateView simpleMultiStateView;
    Unbinder unbinder;

    private int page = 0;
    private List<OrderForm> allDataList = new ArrayList<>();
    OrderAdapter adapter;
    private boolean isLoadMore = false;
    private String carNumber = "";
    public boolean isRefresh = false;


    @Override
    protected int getContentView() {
        return R.layout.layout_recycler;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initStateView();

        mPtrFrameLayout.disableWhenHorizontalMove(true);

        mPtrFrameLayout.setPtrHandler(new PtrHandler() {

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, dataRyc, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                isRefresh = true;
                doRefresh();
            }
        });

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new OrderAdapter(R.layout.item_order_manage, allDataList);
        dataRyc.setAdapter(adapter);

        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.empty_view, null);
        TextView textView = emptyView.findViewById(R.id.content);
        textView.setText("暂无预约单哦!");
        adapter.setEmptyView(emptyView);

        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);
        AppApi.getOrderList(carNumber, 2,"0", page, this, 0, getActivity());
    }

    public void doRefresh() {
        page = 0;
        AppApi.getOrderList(carNumber, 2,"0", page, this, 0, getActivity());
    }

    @Override
    public void handlerSuccessData(String result, int tag) {

        switch (tag) {
            case 0:
                OrderFormList workFormList = JSON.parseObject(result, OrderFormList.class);
                List<OrderForm> dataBeanList = workFormList.getResultList();
                showSuccess();
                if (isRefresh) {
                    isRefresh = false;
                    mPtrFrameLayout.refreshComplete();
                }

                if (page == 0) {
                    allDataList.clear();
                }
                allDataList.addAll(dataBeanList);
                if (allDataList.size() < 10) {
                    adapter.setEnableLoadMore(false);
                } else {
                    adapter.setEnableLoadMore(true);
                }
                if (isLoadMore) {
                    adapter.loadMoreComplete();
                    if (dataBeanList.size() == 0) {
                        adapter.loadMoreEnd();
                    }
                    isLoadMore = false;
                }
                adapter.notifyDataSetChanged();
                break;
        }
    }


    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.getOrderList(carNumber, 2, "0",page, this, 0, getActivity());
    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, ReachActivity.class);
        intent.putExtra("data", allDataList.get(position));
        startActivity(intent);
    }

    private void initStateView() {
        if (simpleMultiStateView == null) return;
        simpleMultiStateView.setEmptyResource(R.layout.view_empty)
                .setRetryResource(R.layout.view_retry)
                .setLoadingResource(R.layout.view_loading)
                .setNoNetResource(R.layout.view_nonet)
                .build()
                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
                    @Override
                    public void onReload() {
                    }
                });
    }

    public void showLoading() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showLoadingView();
        }
    }

    public void showSuccess() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showContent();
        }
    }

    public void showFaild() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showErrorView();
        }
    }

    public void showNoNet() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showNoNetView();
        }
    }
}
